﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class CourseNoticeBLL
    {
        public static IQueryable<CourseNoticeInfo> GetList(string search, DateTime? startDate, DateTime? endDate)
        {
            using (var dal = new CourseNoticeDAL(Settings.ConnectionName))
            {
                var re = dal.SelectQueryable(o => true);
                if (!string.IsNullOrEmpty(search))
                    re = re.Where(o => o.NoticeContent.Contains(search));
                if (startDate.HasValue)
                {
                    re = re.Where(o => o.NoticeTime > startDate.Value);
                }
                if (endDate.HasValue)
                {
                    endDate = endDate.Value.AddDays(1);
                    re = re.Where(o => o.NoticeTime < endDate.Value);
                }
                return re;
            }
        }

        public static CourseNoticeInfo Get(int id)
        {
            using (var dal = new CourseNoticeDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Edit(CourseNoticeInfo info)
        {
            using (var dal = new CourseNoticeDAL(Settings.ConnectionName))
            {
                return dal.Update(info) != null;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new CourseNoticeDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }

        public static bool Add(CourseNoticeInfo info)
        {
            using (var dal = new CourseNoticeDAL(Settings.ConnectionName))
            {
                return dal.Add(info) != null;
            }
        }

    }
}
